package com.example.demo.controller;

import com.example.demo.pojo.PageResult;
import com.example.demo.pojo.Result;
import com.example.demo.pojo.dto.GoodsPageDTO;
import com.example.demo.pojo.dto.GoodsQueryDTO;
import com.example.demo.pojo.entity.Goods;
import com.example.demo.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/polycystic/goods")
public class GoodsController {

    @Autowired
    private GoodsService goodsService;

    @GetMapping("/list")
    public Result<List<Goods>> list(GoodsQueryDTO goodsQueryDTO){
        List<Goods> list=goodsService.list(goodsQueryDTO);
        return Result.success(list);
    }

    @GetMapping("/{id}")
    public Result<Goods> getGoodsInfo(@PathVariable Long id){
        Goods goods=goodsService.getGoodsInfo(id);
        return Result.success(goods);
    }

    @PostMapping("/add")
    public Result add(@RequestBody Goods goods){
        goodsService.add(goods);
        return Result.success();
    }

    @DeleteMapping("/delete")
    //前端传过来的ids为字符串，可以使用@RequestParam注解（Spring MVC框架解析）来封装到List<Long> ids中
    public Result delete(@RequestParam List<Long> ids){
        goodsService.batchDelete(ids);
        return Result.success();
    }


    @PutMapping("/update")
    public Result update(@RequestBody Goods goods){
        goodsService.update(goods);
        return Result.success();
    }

    @PostMapping("/page")
    public Result<PageResult> page(@RequestBody GoodsPageDTO goodsPageDTO){
        PageResult pageResult=goodsService.pageQuery(goodsPageDTO);
        return Result.success(pageResult);
    }
}
